package com.zhdl.hardware.plc.siemens.exc;

import com.project.command.faq.event.behavior.BehaviorPerform;
import com.zhdl.hardware.plc.siemens.repository.SiemensPortCmdHandleDao;
import com.zhdl.modules.tools.CommonCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service("SiemensPortReceiveHandelImpl")
public class SiemensPortReceiveHandelImpl implements SiemensPortCmdHandleDao {
    @Resource
    public BehaviorPerform behaviorPerform;

    public Map<String, SiemensReceiveHandelDaoImpl> daoMap = new ConcurrentHashMap<>();

    @Resource
    public ThreadPoolTaskScheduler taskScheduler1;

    /**
     * 初始化网口
     *
     * @param port 网口地址
     */
    public void addPort(String port) {
        SiemensReceiveHandelDaoImpl siemensReceiveHandelDao = new SiemensReceiveHandelDaoImpl(port, behaviorPerform);
        daoMap.put(port, siemensReceiveHandelDao);
    }

    /**
     * 读取plc的值
     *
     * @param port     网口IP
     * @param address  寄存器地址
     * @param count    寄存器个数
     * @param callback 回调
     */
    @Override
    public void raedValueByAddress(String port, String address, int count, CommonCallback<String> callback) {
        daoMap.get(port).raedValueByAddress(address, count, callback);
    }

    /**
     * 写入plc的值
     *
     * @param port    网口IP
     * @param address 寄存器地址
     * @param value   寄存器值
     */
    @Override
    public void writeValueByAddress(String port, String address, String value) {
        daoMap.get(port).writeValueByAddress(address, value);
    }

    /**
     * 写入plc的值
     *
     * @param port    网口IP
     * @param address 寄存器地址
     * @param value   寄存器值
     */
    @Override
    public void writeValuesByAddress(String port, String address, String value) {
        daoMap.get(port).writeValuesByAddress(address, value);
    }

    /**
     * 读取plc的值
     *
     * @param port     网口IP
     * @param address  寄存器地址
     * @param count    寄存器个数
     * @param callback 回调
     */
    @Override
    public void raedValueByAddress(String port, String slaveId, String address, int count, CommonCallback<String> callback) {
        daoMap.get(port).raedValueByAddress(slaveId, address, count, callback);
    }

    /**
     * 写入plc的值
     *
     * @param port    网口IP
     * @param address 寄存器地址
     * @param value   寄存器值
     */
    @Override
    public void writeValueByAddress(String port, String slaveId, String address, String value) {
        daoMap.get(port).writeValueByAddress(slaveId,address, value);
    }

    /**
     * 写入plc的值
     *
     * @param port    网口IP
     * @param address 寄存器地址
     * @param value   寄存器值
     */
    @Override
    public void writeValuesByAddress(String port, String slaveId, String address, String value) {
        daoMap.get(port).writeValuesByAddress(slaveId,address, value);
    }
}
